/*
 *  /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;
 * http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
*/

//    The page gives doctor the functionality to bid .      
//    The data will be inserted/updated to the tbl_bidding.                                
//    Author: Rupinder Saini                                                                    
//    Date: May 15,2006 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;

//public partial class Doctor_bidding : System.Web.UI.Page
public partial class Doctor_subscription : System.Web.UI.Page
{
    // creating an instance of manager class
    Manager mgr = new Manager(true,"ConnectionString");
    protected void Page_Load(object sender, EventArgs e)
    {
        //btn_bid.Attributes.Add("onclick", "return chk()");
      // check if doctor logged in
      localFunction drchk= new localFunction();
        drchk.checkDoctorLogin(true);

        if (!Page.IsPostBack)
        {             
           // pre - fill the page contents
            loadpagecontents();
        }
        else
        {
            Page.Validate();
        }
    }

    /// <summary>
    /// to get the minimum and highest bid for the speciality
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
 
    protected void ddl_spec_SelectedIndexChanged(object sender, EventArgs e)
    {
        // get the details( max , min bids) acc to the speciality selected
        DataSet ds_bid = new DataSet();
        try
        {
            SqlParameter[] sqlParam = new SqlParameter[1];
            sqlParam[0] = new SqlParameter("@spec_id", SqlDbType.Int);
            sqlParam[0].Value = ddl_spec.SelectedItem.Value;
            ds_bid = mgr.ExecuteQuery("get_minmaxbid", sqlParam, "tbl_bid");
            //if (ds_bid.Tables[0].Rows.Count > 0)
            //{
            //    //code commented to disable bidding process :K, 13 October 2006
            //    //lbl_highbid.Text = ds_bid.Tables[0].Rows[0]["maxbid"].ToString();
            //    //lbl_minbid.Text = ds_bid.Tables[0].Rows[0]["Minbid"].ToString();
            //}
        }
        catch (Exception ex)
        {
            lbl_fail.Visible = true;
            lbl_fail.Text = ex.ToString();
            lbl_fail.ForeColor = System.Drawing.Color.Red;
        }
    }


    /// <summary>
    /// insert the bid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_bid_Click(object sender, ImageClickEventArgs e)
    {
       //CODE COMMENTED TO DISABLE THE BIDDING FEATURE :K, 13 OCTOBER 2006
        //if (String.Compare(ddl_level.SelectedItem.Text, "National") != 0) //(txt_lvl.Visible = true)
        //{
        //    rval_lvl.Enabled = true;
        //    rval_lvl.Validate();
        //}
        //rval_amt.Enabled = true;
        //rval_amt.Validate();


        if (Page.IsValid)
        {

            //K: THIS LINE IS ADDED TO PUT THE APPROPIATE VALUE IN THE 
            //  TXTBID TEXTBOX, ACCORDING TO THE SELECTION OF THE RADIO BUTTONS
            // 4 NOVEMBER 2006
            txtBid.Text = radioSubscription.SelectedValue.ToString();

            //K: 13 October 2006, Code commented to remove the bidding process

            //decimal min = Convert.ToDecimal(lbl_minbid.Text.ToString().Trim());
            //decimal urbid = (Convert.ToDecimal(txt_bid.Text.ToString().Trim()) > 0 ? (Convert.ToDecimal(txt_bid.Text.ToString().Trim())) : 1);
         
            // check if the bid is in increments of 5
            //decimal result = (urbid - min) % 5;
           
            // bid amt shd be greater than equal to the min bid amount set by the admin

            //condition commented for disabling bidding feature :k
            //if (urbid >= min)
            //{
                string errorMessage = String.Empty;
                string filena = String.Empty;
                // upload the doctor's image
                if (fupl_photo.HasFile)
                {
                     errorMessage = mgr.UpLoadFile(fupl_photo, Server.MapPath("../images/Dr_images/"), ".jpg|.bmp|.gif|.jpeg", 512000);
                     if (errorMessage.Length > 0)
                         lbl_err.Text = errorMessage;
                     else
                         filena = fupl_photo.FileName;
                }
                
                // insert the bid
                //if ((result == 0) && (errorMessage.Length==0)) 
            //there is no variable result because the bidding process has been disabled :k
                if (errorMessage.Length == 0)
                {
                    lbl_err.Visible = false;
                   
                    // CODE COMMENTED TO DISABLE THE BIDDING PROCESS :K, 13 OCTOBER 2006
                    // Premium features
                    // if ((ddl_spec.Enabled = true) && (Convert.ToInt32(lbl_bid.Text.Trim())==0))    // add new bid

                    if (ddl_spec.Enabled == true)
                    {
                        try
                        {
                            SqlParameter[] sqlParam = new SqlParameter[10];
                            sqlParam[0] = new SqlParameter("@Dr_Id", SqlDbType.Int);
                            sqlParam[1] = new SqlParameter("@Bid_Amount", SqlDbType.Money);
                            sqlParam[2] = new SqlParameter("@Speciality_Id", SqlDbType.Int);
                            sqlParam[3] = new SqlParameter("@bid_level", SqlDbType.VarChar, 15);
                            sqlParam[4] = new SqlParameter("@bid_leveldet", SqlDbType.VarChar, 50);
                            sqlParam[5] = new SqlParameter("@renew", SqlDbType.Bit);

                            sqlParam[6] = new SqlParameter("@photo", SqlDbType.VarChar, 100);
                            sqlParam[7] = new SqlParameter("@website", SqlDbType.VarChar, 200);
                            //sqlParam[8] = new SqlParameter("@prof_desc", SqlDbType.VarChar, 1000);

                            //K: size changed to 1500
                            sqlParam[8] = new SqlParameter("@prof_desc", SqlDbType.VarChar, 2000);
                            
                            //K: THIS PARAMETER IS NEWLY ADDED 
                            sqlParam[9] = new SqlParameter("@bidtype", SqlDbType.VarChar, 1);

                            sqlParam[0].Value = Session["userID"];
                            //sqlParam[1].Value = txt_bid.Text;
                            sqlParam[1].Value = txtBid.Text;
                            sqlParam[2].Value = ddl_spec.SelectedItem.Value;
                            sqlParam[3].Value = ddl_level.SelectedItem.Text;
                            
                            sqlParam[6].Value = filena;
                            sqlParam[7].Value = txt_web.Text;
                            sqlParam[8].Value = txt_profdesc.Text;

                            if (txt_lvl.Visible == true)
                            { sqlParam[4].Value = txt_lvl.Text; }
                            else
                            { sqlParam[4].Value = "N/A"; }
                            
                            //CODE COMMENTED TO DISABLE THE BIDDING PROCESS :K, 13 OCTOBER 2006
                            //if (chk_renew.Checked)
                            //{
                            sqlParam[5].Value = true;
                            //}
                            //else
                            //{
                            //    sqlParam[5].Value = false;
                            //}

                            int bid_val = 0;
                            
                            if (radioSubscription.SelectedValue == "20")
                            { 
                                bid_val = 2;
                                sqlParam[9].Value = "M";
                            }
                            else
                            {
                                bid_val = 1;
                                sqlParam[9].Value = "Y";
                            }

                            mgr.ExecuteUpdate("insert_bid", sqlParam);

                            //SEND TO THE PAYMENT GATEMENT 

                            //if (radioSubscription.SelectedValue == "20")
                            //{ Response.Redirect("PaypalCheckoutSubs.aspx"); }
                            //else
                            //{ Response.Redirect("PaypalCheckout.aspx?radioSubscription=" + radioSubscription.SelectedValue.ToString()); }
                            Response.Redirect("PaypalCheckout.aspx?bidingtype=" + bid_val.ToString()); 
                        }
                        catch (Exception ex)
                        {
                            lbl_fail.Visible = true;
                            lbl_fail.Text = ex.ToString();
                            lbl_fail.ForeColor = System.Drawing.Color.Red;
                        }
                    }
                    else
                    {                        
                        //UPDATING PROFILE: K

                        SqlParameter[] sqlParamUpd = new SqlParameter[8];
                        sqlParamUpd[0] = new SqlParameter("@bid_id", SqlDbType.Int);
                        sqlParamUpd[1] = new SqlParameter("@Bid_Amount", SqlDbType.Money);
                        sqlParamUpd[2] = new SqlParameter("@renew", SqlDbType.Bit);
                        sqlParamUpd[3] = new SqlParameter("@trx_amt", SqlDbType.Money);

                        sqlParamUpd[4] = new SqlParameter("@photo", SqlDbType.VarChar, 100);
                        sqlParamUpd[5] = new SqlParameter("@website", SqlDbType.VarChar, 200);
//                        sqlParamUpd[6] = new SqlParameter("@prof_desc", SqlDbType.VarChar, 1000);
                        sqlParamUpd[6] = new SqlParameter("@prof_desc", SqlDbType.VarChar, 2000);
                        sqlParamUpd[7] = new SqlParameter("@dr_id", SqlDbType.Int);

                        sqlParamUpd[0].Value = 0; //lbl_bid.Text;
                        //sqlParamUpd[1].Value = txt_bid.Text;
                        sqlParamUpd[1].Value = 0; // txtBid.Text;
                        sqlParamUpd[2].Value = false;
                        sqlParamUpd[3].Value = 0;
                        sqlParamUpd[4].Value = filena;
                        sqlParamUpd[5].Value = txt_web.Text;
                        sqlParamUpd[6].Value = txt_profdesc.Text;
                        sqlParamUpd[7].Value = Convert.ToInt32(Session["userid"]);

                        mgr.ExecuteUpdate("update_bid", sqlParamUpd);
                    }
/*
                    else  //update the existing bid
                    {                        
                        //if ((Convert.ToInt32(txt_bid.Text)) >= (Convert.ToInt32(lbl_initialamt.Text)))
                        if ((Convert.ToInt32(txtBid.Text)) >= (Convert.ToInt32(lbl_initialamt.Text)))
                        {
                            
                            try
                            {
                                SqlParameter[] sqlParamUpd = new SqlParameter[7];
                                sqlParamUpd[0] = new SqlParameter("@bid_id", SqlDbType.Int);
                                sqlParamUpd[1] = new SqlParameter("@Bid_Amount", SqlDbType.Money);
                                sqlParamUpd[2] = new SqlParameter("@renew", SqlDbType.Bit);
                                sqlParamUpd[3] = new SqlParameter("@trx_amt", SqlDbType.Money);

                                sqlParamUpd[4] = new SqlParameter("@photo", SqlDbType.VarChar, 100);
                                sqlParamUpd[5] = new SqlParameter("@website", SqlDbType.VarChar, 200);
                                sqlParamUpd[6] = new SqlParameter("@prof_desc", SqlDbType.VarChar, 1000);

                                sqlParamUpd[0].Value = lbl_bid.Text;
                                //sqlParamUpd[1].Value = txt_bid.Text;
                                sqlParamUpd[1].Value = txtBid.Text;
                                sqlParamUpd[4].Value = filena;
                                sqlParamUpd[5].Value = txt_web.Text;
                                sqlParamUpd[6].Value = txt_profdesc.Text;


                                //if ((Convert.ToInt32(txt_bid.Text)) > (Convert.ToInt32(lbl_initialamt.Text)))
                                // CODE COMMENTED TO DISABLE THE BIDDING PROCESS :K, 13 OCT 2006
                                //if ((Convert.ToInt32(txtBid.Text)) > (Convert.ToInt32(lbl_initialamt.Text)))
                                //{
                                //    //int trx_amount = (Convert.ToInt32(txt_bid.Text)) - (Convert.ToInt32(lbl_initialamt.Text));
                                //    int trx_amount = (Convert.ToInt32(txtBid.Text)) - (Convert.ToInt32(lbl_initialamt.Text));
                                //    sqlParamUpd[3].Value = trx_amount;
                                //}
                                //else
                                //COMMENT ENDS HERE
                                {
                                    sqlParamUpd[3].Value = 0;
                                }

                                //CODE COMMENTED TO DISABLE THE BIDDING FUNCTION :K, 13, OCTOBER 2006
                                //if (chk_renew.Checked)
                                //{
                                sqlParamUpd[2].Value = true;
                                //}
                                //else
                                //{
                                //    sqlParamUpd[2].Value = false;
                                //}                                                          
                                mgr.ExecuteUpdate("update_bid", sqlParamUpd);

                                //if ((Convert.ToInt32(txt_bid.Text)) > (Convert.ToInt32(lbl_initialamt.Text)))
                                //CODE COMMENTED TO DISABLE THE BIDDING PROCESS, :K 13 OCT 2006
                                //if ((Convert.ToInt32(txtBid.Text)) > (Convert.ToInt32(lbl_initialamt.Text)))
                                //    Response.Redirect("PaypalCheckout.aspx");//?id=" + lbl_bid.Text);
                                //else
                                //COMMENTING ENDS HERE
                                {
                                    lbl_fail.Visible = true;
                                    lbl_fail.Text = "Your information has been updated";
                                    ddl_spec.Enabled = false;
                                }
                            }
                            catch (Exception ex)
                            {
                                lbl_fail.Visible = true;
                                lbl_fail.Text = ex.ToString();
                                lbl_fail.ForeColor = System.Drawing.Color.Red;
                            }
                        }
                        else
                        {
                            lbl_err.Visible = true;
                            lbl_err.Text = "You can only increase the bid amount.";
                        }
                    }
 */
                }
                else
                {
                   
                    if (errorMessage.Length != 0)
                    {
                        lbl_fail.Visible = true;
                        lbl_fail.Text = errorMessage;
                    }
                    else
                    lbl_err.Visible = true;
                    lbl_err.Text = "Bid must be raised in multiples of 5";
                }
            //}                
            //else
            //{
            //    lbl_err.Visible = true;
            //    lbl_err.Text = "Your Bid must be greater than the minimum bid";
            //}
            //if block commented for disabling bidding process :K
        }
    }

    #region general functions

    /// <summary>
    /// pre - fill the page controls 
    /// </summary>

    public void loadpagecontents()
    {
        
        mgr.BindList("select id,name from tbl_speciality order by name", "name", "id", ddl_spec, "");        

        ddl_level.Items.Add(new ListItem("National", "1"));
        ddl_level.Items.Add(new ListItem("State", "2"));
        ddl_level.Items.Add(new ListItem("Zip", "3"));
        // get the speciality for the doctor

        DataSet ds_drbid = new DataSet();
        try
        {
            SqlParameter[] sqlParam = new SqlParameter[1];
            sqlParam[0] = new SqlParameter("@dr_id", SqlDbType.Int);
            sqlParam[0].Value = Session["userID"];
            // fetch the bid details
            ds_drbid = mgr.ExecuteQuery("get_biddingdetailsbydrid", sqlParam, "tbl_bid");
            if (ds_drbid.Tables[0].Rows.Count > 0)
            {
                //CODE COMMENTED TO DISABLE THE BIDDING FUNCTION :K, 13, OCTOBER 2006
                //lbl_highbid.Text = ds_drbid.Tables[0].Rows[0]["maxbid"].ToString();
                //lbl_minbid.Text = (ds_drbid.Tables[0].Rows[0]["Minbid"].ToString().Trim().Length == 0) ? "0": ds_drbid.Tables[0].Rows[0]["Minbid"].ToString().Trim();

                if (ds_drbid.Tables[0].Rows.Count>0)
                {
                    //Response.Write(ds_drbid.Tables[0].Rows[0]["spec_id"].ToString());
                    ddl_spec.Items.FindByValue(ds_drbid.Tables[0].Rows[0]["spec_id"].ToString()).Selected = true;                    
                    lbl_initialamt.Text = ds_drbid.Tables[0].Rows[0]["Bid_Amount"].ToString();
                    lbl_bid.Text = ds_drbid.Tables[0].Rows[0]["bid_id"].ToString();
                }

                //CODE COMMENTED TO DISABLE THE BIDDING FUNCTION :K, 13, OCTOBER 2006
                //txt_bid.Text = ds_drbid.Tables[0].Rows[0]["Bid_Amount"].ToString();                
                //if (Convert.ToInt32(txt_bid.Text) != 0)  //txt_bid.Text
                //{
                if (Convert.ToInt32(ds_drbid.Tables[0].Rows[0]["Bid_Amount"].ToString()) != 0)  
                {                
                    ddl_level.Items.FindByText(ds_drbid.Tables[0].Rows[0]["bid_level"].ToString()).Selected = true;
                    txt_lvl.Text = ds_drbid.Tables[0].Rows[0]["bid_leveldet"].ToString();

                    //if (String.Compare(ds_drbid.Tables[0].Rows[0]["renew"].ToString(), "False") == 0)
                    //{
                    //    chk_renew.Checked = false;
                    //}
                    
                    ddl_spec.Enabled = false;
                    radioSubscription.Enabled = false;
                    reqSubscription.Enabled = false;
                    ddl_level.Enabled = false;
                    
                    //K: THIS TEXT WAS CHANGED AS PER CLIENT'S REQUIREMENT
                    //15 November 2006

                    //lbl_drbid.Text = lbl_drbid.Text + " : Raise your bid";
                }                
            }            
                //CODE COMMENTED TO DISABLE THE BIDDING FUNCTION :K 4, NOVEMBER 2006
                //ConfigurationManager.AppSettings["FixedBidAmount"].ToString();
        }
        catch (Exception ex)
        {
            lbl_fail.Visible = true;
            lbl_fail.Text = ex.ToString();
            lbl_fail.ForeColor = System.Drawing.Color.Red;
        }
        
    }

   
 #endregion


    // if state or zip code selected 
    protected void ddl_level_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (String.Compare(ddl_level.SelectedItem.Text, "National") == 0)
        {
            txt_lvl.Visible = false;
        }
        else
        {
            txt_lvl.Visible = true;
        }
    }


    protected void btn_unpaid_Click(object sender, EventArgs e)
    {
        try
        {
            SqlParameter[] sqlParam = new SqlParameter[1];
            sqlParam[0] = new SqlParameter("@dr_id", SqlDbType.Int);
            sqlParam[0].Value = Session["userID"];
            // fetch the bid details
             DataSet ds_drbid = mgr.ExecuteQuery("getbiddetailsbybidid", sqlParam, "tbl_bid");
             if (ds_drbid.Tables[0].Rows.Count > 0)
             {
                 if (Convert.ToDouble(ds_drbid.Tables[0].Rows[0]["Trx_Amount"].ToString()) > 0)
                 {
                     Response.Redirect("PaypalCheckout.aspx");
                 }
                 else
                 {
                     lbl_fail.Visible = true;
                     lbl_fail.Text = "You have no unpaid bids";
                 }
             }
             else
             {
                 lbl_fail.Visible = true;
                 lbl_fail.Text = "You have no unpaid bids";
             }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}
